package com.zhou.common.mybatis.template.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.zhou.common.mybatis.template.entity.FavAddress;
import com.zhou.common.mybatis.template.entity.FavGroup;
import com.zhou.common.mybatis.template.mapper.FavGroupMapper;
import com.zhou.common.mybatis.template.model.vo.FavAddressVo;
import com.zhou.common.mybatis.template.model.vo.FavGroupAllVo;
import com.zhou.common.mybatis.template.service.IFavGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 收藏组 服务实现类
 * </p>
 *
 * @author zxf
 * @since 2023-04-15
 */
@Service
public class FavGroupServiceImpl extends ServiceImpl<FavGroupMapper, FavGroup> implements IFavGroupService {

    @Autowired
    private FavAddressServiceImpl favAddressService;

    @Override
    public List<FavGroupAllVo> queryALL() {

        List<FavGroupAllVo> result = Lists.newArrayList();
        List<FavGroup> favGroups = list();
        favGroups.forEach(t -> {
                    FavGroupAllVo   favGroupAllVo=new FavGroupAllVo();
                    favGroupAllVo.setGroupName(t.getName());
                    favGroupAllVo.setId(t.getId());
                    LambdaQueryWrapper<FavAddress> eq = Wrappers.<FavAddress>lambdaQuery()
                            .eq(FavAddress::getGroupId, t.getId());
                    List<FavAddress> list = favAddressService.list(eq);
                    List<FavAddressVo> favAddressAos=Lists.newArrayList();
                    list.forEach(tt->{
                        FavAddressVo addressVo=new FavAddressVo();
                        addressVo.setAddress(tt.getAddress());
                        addressVo.setDescription(tt.getDescription());
                        addressVo.setId(tt.getId());
                        favAddressAos.add(addressVo);
                    });
                    favGroupAllVo.setAddresses(favAddressAos);
                    result.add(favGroupAllVo);
                }
        );
        return result;
    }

    @Override
    public void delete(Long id) {
        LambdaQueryWrapper<FavAddress> eq = Wrappers.<FavAddress>lambdaQuery()
                .eq(FavAddress::getGroupId, id);
        int count = favAddressService.count(eq);
        if (count==0){
            removeById(id);
        }
    }
}
